// XiimClient.cs by steb at 4:05 PM 8/10/2008
//
//
// Copyright (C) 2008 Steven Allen
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
//

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;using System.Data;

namespace Xiim
{
	public class XiimClient
	{
		private TcpClient client = new TcpClient();
		private NetworkStream stream = null;
		private string me = "";
		public string whoami()
		{
			return me;
		}

		public string receiveData()
		{
			if (getConnected())
			{
				if (stream.DataAvailable)
				{
					Byte[] data = new Byte[256];
					Int32 bytes = stream.Read(data, 0, data.Length);
					return "" + System.Text.Encoding.ASCII.GetString(data, 0, bytes) + "\r\n";
				}
				else
				{
					return "";
				}
            }
			else
			{
				return ":: Connection Closed ::";
			}
		}

        public void sendData(string strData)
        {
            if (getConnected())
            {
                try
                {
                    Byte[] btData = System.Text.Encoding.ASCII.GetBytes(strData);
                    stream.Write(btData, 0, btData.Length);
                }
                catch (System.IO.IOException)
                {
                    disconnect();
                }
            }
        }

        public void disconnect()
        {
			stream.Close();
			client.Close();
		}
		
		public bool connect(string conAddress)
		{
			try
			{
				string endPt = conAddress;
				bool parsed = false;
				Int32 port = 13913;
				if (conAddress.Contains("@") && !conAddress.StartsWith("@") && !conAddress.EndsWith("@"))
					{
					string[] addressArray = conAddress.Split('@');
					if (addressArray.Length > 2)
					{
						return false;
					}
					me = addressArray[0];
					endPt = addressArray[1];
					parsed = true;
				}

				if (endPt.Contains(":") && !endPt.StartsWith(":") && !endPt.EndsWith(":"))
				{
					string[] addressArray = endPt.Split(':');
					if (addressArray.Length > 2)
					{
						return false;
					}
					
					try
					{
						port = Int32.Parse(addressArray[1]);
					} catch {
						return false;
					}
					endPt = addressArray[0];
					parsed = true;
			}
				if (!parsed)
                {
                    endPt = conAddress;
                    me = System.Net.Dns.GetHostName();
				}
                // Create client
                client = new TcpClient();
                client.Connect(endPt, port);
                // Open stream
                stream = client.GetStream();
				sendData(me);
                return true;
            }
            catch (ArgumentNullException)
            {
                return false;
            }
            catch (SocketException)
            {
                return false;
            }
        }

        public bool getConnected()
        {
            return client.Connected;
        }
    }
}
